package com.hn.shopadmin.controller;

import com.hn.shopadmin.dto.RoleMenuDTO;
import com.hn.shopadmin.service.RoleMenuService;
import com.hn.kiss.modules.sys.shopcommon.result.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.List;

@Api(tags = "RoleMenuController", description = "角色菜单关联业务接口")
@RestController
@RequestMapping("/roleVsMenu")
@Validated
public class RoleMenuController {
    @Autowired
    RoleMenuService roleMenuService;

    @ApiOperation("角色菜单关联添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "menuIds", value = "菜单ids,多个以','隔开", required = true, dataType = "String", paramType = "query")
    })
    @PostMapping("/add")
    public CommonResult add(@NotBlank(message = "roleId不能为空") String roleId,
                            @NotBlank(message = "菜单id不能为空") String menuIds){
        String[] menu_ids = menuIds.split(",");
        int count = roleMenuService.add(roleId, menu_ids);
        if (count > 0){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("所有菜单信息,以及和角色对应关系")
    @ApiImplicitParam(name = "roleId", value = "角色id", required = true, dataType = "String", paramType = "query")
    @GetMapping("/allMenuVsRole")
    public CommonResult<List<RoleMenuDTO>> allMenuVsRole(@NotBlank(message = "roleId不能为空") String roleId){
        List<RoleMenuDTO> roleMenuDTOS = roleMenuService.allMenuVsRole(roleId);
        return CommonResult.success(roleMenuDTOS);
    }


}
